define(function (require, exports, module) {
	var aw = require("ajaxwrapper");
	var Dialog = require("dialog");
	var enmu = require("enums");
	var store =require("store");
	var activeUser = store.get("user");
	var Form = require("form");
	var getSignupCharge = require("../../../checkoutsettlement/src/store/getSignupCharge");
	var total = require("../../../checkoutsettlement/src/handler/total");
	var alltotal = require("./alltotal");
	
	module.exports = {
        subnav: {
        	//保存
        	handleSaveClick: function (widget, data) {
            },
        },
        grid: {
            handleEditClick: function (widget, data) {
            	var paymentMethods = [];
				for(var i = 0; i < data.paymentMethods.length; i++){
					paymentMethods[i] = data.paymentMethods[i].value;
				}
            	var items = [{
					name:"payer",
					label:"付款单位或个人",
					defaultValue:data.planItem
				},{
					name:"content",
					label:"款项内容",
				},{
					name:"checkInDate",
					label:"入住日期",
					type:"date",
					defaultValue:moment(data.checkInDate).format("YYYY-MM-DD"),
					readonly : true,
				},{
					name:"checkOutDate",
					label:"退房日期",
					type:"date",
					defaultValue:moment(data.checkOutDate).format("YYYY-MM-DD"),
					readonly : true,
				},{
					name:"balance",
					label:"结算金额",
					defaultValue:data.balance.toFixed(2),
					readonly : true,
				},{
					name:"paymentMethod",
					label:"付款方式",
					defaultValue:paymentMethods,
					readonly : true,
				},{
					name:"isInvoice",
					label:"是否开票",
					type:"select",
					options:[{
						key:"true",
						value:"是"
					},{
						key:"false",
						value:"否"
					}],
					defaultValue:"true"
				},{
					name:"invoiceDate",
					label:"开票日期",
					type:"date",
					defaultValue:moment(data.checkOutDate).format("YYYY-MM-DD"),
				},{
					name:"payee",
					label:"收款单位",
					defaultValue:activeUser.servicePoint.name,
					"break":true
				},{
					name:"user",
					label:"确认人",
					defaultValue:activeUser.name,
					readonly : true,
				},{
					name:"confirmDate",
					label:"确认日期",
					type:"date",
					defaultValue:moment().format("YYYY-MM-DD"),
					readonly : true,
				}]
            	
            	if(data.type == "直属入住联盟"){
                	aw.ajax({
        				url : "api/alliancesettlement/query",
        				data : {
        					pkAllianceSettlement:data.pk,
        					fetchProperties:"*,confirmUser.*,confirmStatus.*,paymentMethods.*"
        				},
        				dataType : "json",
        				success : function(datas){
        					var remarkForm =new Form({
        						parentNode:".J-form-dialog",
        						autoRender : false,
        		        		model:{
        		        			layout :"2_1",
        							id:"remarkForm",
        							defaultButton:false,
        							items:items
        						 }
        					});
        					widget.set("remarkForm",remarkForm);
        					
        					Dialog.showComponent(remarkForm,{
        						defaultButton:false,
        						title: "团队退房结算单",
        						buttons :[{
        							id:"save",
        							text:"保存",
        							className:"btn-primary",
        							handler:function(){
        								if(remarkForm.valid()){
        									var FormData = remarkForm.getData();
        									var saveData = {};
        									saveData.pkAllianceSettlement = data.pk;
        									saveData.version = data.version;
        									saveData.payer = FormData.payer;
        									saveData.content = FormData.content;
        									saveData.isInvoice = FormData.isInvoice;
        									saveData.invoiceDate = FormData.invoiceDate;
        									saveData.payee = FormData.payee;
        									aw.saveOrUpdate("api/checkoutsettlement/allianceconfirm",aw.customParam(saveData),function(saveData){
        										Dialog.close();
        					          			widget.get("grid").refresh();
        					       			});
        								};
        							}
        						},{
        							id:"close",
        							text:"取消",
        							handler:function(){
        								Dialog.close();
        							}
        						}]
        					});
        					var paymentMethods = [];
        					for(var i = 0; i < data.paymentMethods.length; i++){
        						paymentMethods[i] = data.paymentMethods[i].value;
        					}
        					remarkForm.setValue("paymentMethod",paymentMethods);
        				}
        			})
            	}else{
                	aw.ajax({
        				url : "api/checkinimplement/query",
        				data : {
        					pkCIImplement:data.pk,
            				fetchProperties:"*,confirmUser.*,confirmStatus.*,paymentMethods.*"
        				},
        				dataType : "json",
        				success : function(datas){
        					var remarkForm =new Form({
        						parentNode:".J-form-dialog",
        						autoRender : false,
        		        		model:{
        		        			layout :"2_1",
        							id:"remarkForm",
        							defaultButton:false,
        							items:items
        						 }
        					});
        					widget.set("remarkForm",remarkForm);
        					Dialog.showComponent(remarkForm,{
        						defaultButton:false,
        						title: "团队退房结算单",
        						buttons :[{
        							id:"save",
        							text:"保存",
        							className:"btn-primary",
        							handler:function(){
        								if(remarkForm.valid()){
        									var FormData = remarkForm.getData();
        									var saveData = {};
        									saveData.pkCIImplement = data.pk;
        									saveData.version = data.version;
        									saveData.payer = FormData.payer;
        									saveData.content = FormData.content;
        									saveData.isInvoice = FormData.isInvoice;
        									saveData.invoiceDate = FormData.invoiceDate;
        									saveData.payee = FormData.payee;
        									aw.saveOrUpdate("api/checkoutsettlement/orgconfirm",aw.customParam(saveData),function(saveData){
        										Dialog.close();
        					          			widget.get("grid").refresh();
        					       			});
        								};
        							}
        						},{
        							id:"close",
        							text:"取消",
        							handler:function(){
        								Dialog.close();
        							}
        						}]
        					});
        					
        					var paymentMethods = [];
        					for(var i = 0; i < data.paymentMethods.length; i++){
        						paymentMethods[i] = data.paymentMethods[i].value;
        					}
        					remarkForm.setValue("paymentMethod",paymentMethods);
        				}
        			})
            	}
            },
            handleShowClick :function (widget, data) {
            	var paymentMethods = [];
				for(var i = 0; i < data.paymentMethods.length; i++){
					paymentMethods[i] = data.paymentMethods[i].value;
				}
            	var items = [{
					name:"payer",
					label:"付款单位或个人",
					defaultValue:data.planItem
				},{
					name:"content",
					label:"款项内容",
				},{
					name:"checkInDate",
					label:"入住日期",
					type:"date",
					defaultValue:moment(data.checkInDate).format("YYYY-MM-DD"),
					readonly : true,
				},{
					name:"checkOutDate",
					label:"退房日期",
					type:"date",
					defaultValue:moment(data.checkOutDate).format("YYYY-MM-DD"),
					readonly : true,
				},{
					name:"balance",
					label:"结算金额",
					defaultValue:data.balance,
					readonly : true,
				},{
					name:"paymentMethod",
					label:"付款方式",
					defaultValue:paymentMethods,
					readonly : true,
				},{
					name:"isInvoice",
					label:"是否开票",
					type:"select",
					options:[{
						key:"true",
						value:"是"
					},{
						key:"false",
						value:"否"
					}],
					defaultValue:"true"
				},{
					name:"invoiceDate",
					label:"开票日期",
					type:"date",
					defaultValue:moment(data.checkOutDate).format("YYYY-MM-DD"),
				},{
					name:"payee",
					label:"收款单位",
					defaultValue:activeUser.servicePoint.name,
					"break":true
				},{
					name:"user",
					label:"确认人",
					defaultValue:activeUser.name,
					readonly : true,
				},{
					name:"confirmDate",
					label:"确认日期",
					type:"date",
					defaultValue:moment().format("YYYY-MM-DD"),
					readonly : true,
				}]
            	
            	if(data.type == "直属入住联盟"){
                	aw.ajax({
        				url : "api/alliancesettlement/query",
        				data : {
        					pkAllianceSettlement:data.pk,
        					fetchProperties:"*,confirmUser.*,confirmStatus.*,paymentMethods.*"
        				},
        				dataType : "json",
        				success : function(datas){
        					var remarkForm =new Form({
        						parentNode:".J-form-dialog",
        						autoRender : false,
        		        		model:{
        		        			layout :"2_1",
        							id:"remarkForm",
        							defaultButton:false,
        							items:items
        						 }
        					});
        					widget.set("remarkForm",remarkForm);
        					
        					Dialog.showComponent(remarkForm,{
        						defaultButton:false,
        						title: "入住联盟结算确认",
        						buttons :[{
        							id:"close",
        							text:"取消",
        							handler:function(){
        								Dialog.close();
        							}
        						}]
        					});
        					
        					remarkForm.setValue("payer",datas[0].payer);
        					remarkForm.setValue("content",datas[0].content);
        					remarkForm.setValue("balance",data.balance);
        					var paymentMethods = [];
        					for(var i = 0; i < data.paymentMethods.length; i++){
        						paymentMethods[i] = data.paymentMethods[i].value;
        					}
        					remarkForm.setValue("paymentMethod",paymentMethods);
        					remarkForm.setValue("isInvoice",datas[0].isInvoice);
        					remarkForm.setValue("invoiceDate",datas[0].invoiceDate);
        					remarkForm.setValue("payee",datas[0].payee);
        					remarkForm.setValue("user",datas[0].confirmUser.name);
        					remarkForm.setValue("confirmDate",datas[0].confirmDate);
        					remarkForm.setDisabled(true);
        				}
        			})
            	}else{
                	aw.ajax({
        				url : "api/checkinimplement/query",
        				data : {
        					pkCIImplement:data.pk,
            				fetchProperties:"*,confirmUser.*,confirmStatus.*,paymentMethods.*"
        				},
        				dataType : "json",
        				success : function(datas){
        					var remarkForm =new Form({
        						parentNode:".J-form-dialog",
        						autoRender : false,
        		        		model:{
        		        			layout :"2_1",
        							id:"remarkForm",
        							defaultButton:false,
        							items:items
        						 }
        					});
        					widget.set("remarkForm",remarkForm);
        					Dialog.showComponent(remarkForm,{
        						defaultButton:false,
        						title: "入住联盟结算确认",
        						buttons :[{
        							id:"close",
        							text:"取消",
        							handler:function(){
        								Dialog.close();
        							}
        						}]
        					});
        					
        					remarkForm.setValue("payer",datas[0].payer);
        					remarkForm.setValue("content",datas[0].content);
        					remarkForm.setValue("balance",data.balance);
        					var paymentMethods = [];
        					for(var i = 0; i < data.paymentMethods.length; i++){
        						paymentMethods[i] = data.paymentMethods[i].value;
        					}
        					remarkForm.setValue("paymentMethod",paymentMethods);
        					remarkForm.setValue("isInvoice",datas[0].isInvoice);
        					remarkForm.setValue("invoiceDate",datas[0].invoiceDate);
        					remarkForm.setValue("payee",datas[0].payee);
        					remarkForm.setValue("user",datas[0].confirmUser.name);
        					remarkForm.setValue("confirmDate",datas[0].confirmDate);
        					remarkForm.setDisabled(true);
        				}
        			})
            	}
            },
            handlerDetailClick: function(widget,data){
            	
            	widget.set("teamType", data.type);
            	
				if(data.type != "直属入住联盟"){
					var form = widget.get("form");
					var endGrid = widget.get("endGrid");
            	aw.ajax({
    				url : "api/checkinimplement/query",
    				data : {
    					pkCIImplement:data.pk,
    					fetchProperties:"*,confirmUser.*,confirmStatus.*,paymentMethods.*," +
    							"groupReserve.*,groupReserve.planItemGroup.*,groupReserve.planItemGroup.planItem.*," +
    							"groupReserve.planItemGroup.planItem.destination.*," +
    							"groupReserve.operator.operator.pkUser,"+
    							"roomPersonalInfos.roomConsumption,"
    							+ "roomPersonalInfos.damagesConsumption,"
    							+ "roomPersonalInfos.roomRegister.room.code,"
    							+ "roomPersonalInfos.roomRegister.room.roomType.name,"
    							+ "roomPersonalInfos.roomRegister.room.orgBuilding.name,"
    							+ "roomPersonalInfos.roomRegister.room.price,"
    							+ "roomPersonalInfos.roomRegister.*,"
    							+ "roomPersonalInfos.signUpRegister.personalInfo.idNumber,"
    							+ "roomPersonalInfos.signUpRegister.personalInfo.name,"
    							+ "roomPersonalInfos.signUpRegister.travelContract.memberCertificates,"
    							+ "roomPersonalInfos.signUpRegister.travelContract.pkTravelContract,"
    							+ "roomPersonalInfos.signUpRegister.travelContract.member.personalInfo.name,"
    							+ "roomPersonalInfos.signUpRegister.travelContract.member.personalInfo.idNumber,"
    							+ "roomPersonalInfos.checkOutSettlement.room," 
    							+ "roomPersonalInfos.checkOutSettlement.version," 
    							+ "roomPersonalInfos.checkOutSettlement.pkCheckOutSettlement,"
    							+ "roomPersonalInfos.checkOutSettlement.checkOutSettlement,"
    							+ "roomPersonalInfos.checkOutSettlement.electric,"
    							+ "roomPersonalInfos.checkOutSettlement.coldWater,"
    							+ "roomPersonalInfos.checkOutSettlement.hotWater,"
    							+ "roomPersonalInfos.checkOutSettlement.telephone,"
    							+ "roomPersonalInfos.checkOutSettlement.food,"
    							+ "roomPersonalInfos.checkOutSettlement.electricOther,"
    							+ "roomPersonalInfos.checkOutSettlement.waterOther,"
    							+ "roomPersonalInfos.checkOutSettlement.foodOther,"
    							+ "roomPersonalInfos.checkOutSettlement.health,"
    							+ "roomPersonalInfos.checkOutSettlement.healthOther,"
    							+ "roomPersonalInfos.checkOutSettlement.holiday,"
    							+ "roomPersonalInfos.checkOutSettlement.traffic,"
    							+ "roomPersonalInfos.checkOutSettlement.pickUp,"
    							+ "roomPersonalInfos.checkOutSettlement.manager,"
    							+ "roomPersonalInfos.checkOutSettlement.other,"
    							+ "roomPersonalInfos.checkOutSettlement.sum,"
    							+ "roomPersonalInfos.checkOutSettlement.expense,"
    							+ "roomPersonalInfos.checkOutSettlement.bedSubsidy,"
    							+ "roomPersonalInfos.checkOutSettlement.bedSubsidyUnit,"
    							+ "roomPersonalInfos.checkOutSettlement.coupon,"
    							+ "roomPersonalInfos.checkOutSettlement.balance,"
    							+ "roomPersonalInfos.checkOutSettlement.nurse,"
    							+ "roomPersonalInfos.checkOutSettlement.property,"
    							+ "roomPersonalInfos.checkOutSettlement.remak,"
    							+ "roomPersonalInfos.pkRoomPersonalInfo,"
    							+ "roomPersonalInfos.roomPerConsumptions,"
    							+ "roomPersonalInfos.roomPerConsumptions.*,"
    							+ "roomPersonalInfos.roomPerConsumptions.goods.*,"
    							+ "roomPersonalInfos.roomRegister.pkRoomRegister,"
    							+ "roomPersonalInfos.signUpRegister.pkSignUpRegister,"
    							+ "roomPersonalInfos.signUpRegister.personalInfo.name,"
    							+ "roomPersonalInfos.signUpRegister.personalInfo.idNumber,"
    							+ "roomPersonalInfos.signUpRegister.personalInfo.sex,"
    							+ "roomPersonalInfos.signUpRegister.personalInfo.mobilePhone,"
    							+ "roomPersonalInfos.signUpRegister.checkOutDate,"
    							+ "roomPersonalInfos.signUpRegister.checkOut,"
    							+ "roomPersonalInfos.signUpRegister.travelContract.productRule.product.name,"
    							+ "roomPersonalInfos.signUpRegister.travelContract.memberCertificates"
    				},
    				dataType : "json",
    				success : function(datas){
   					var liveDays = (moment(datas[0].groupReserve.checkOutDate).startOf("day").valueOf() - 
   							moment(datas[0].groupReserve.checkInDate).startOf("day").valueOf())/1000/60/60/24;
   					widget.set("pkGroupReserve",datas[0].groupReserve.pkGroupReserve);
    					form.setData({
    						startDate: datas[0].groupReserve.planItemGroup.planItem.startDate,
    						destination: datas[0].groupReserve.planItemGroup.planItem.destination.pkDestination,
    						destinationName: datas[0].groupReserve.planItemGroup.planItem.destination.name,
    						checkInDate: datas[0].groupReserve.checkInDate,
    						checkOutDate: datas[0].groupReserve.checkOutDate,
    						signUpNum: datas[0].signUpNum,
    						statementDate: datas[0].statementDate,
    						user: datas[0].groupReserve.operator.operator.pkUser,
    						liveDays: liveDays
    					});
    					endGrid.setData([]);
    					widget.get("totalGrid").setDisabled(true);
    					
    					for(var i=0;i<datas[0].paymentMethods.length;i++){
    						if(datas[0].paymentMethods[i].key=="OnCredit"){
    							var onCreditForm = widget.get("onCreditForm");
    							onCreditForm.setValue("company",datas[0].company);
    							onCreditForm.setValue("perName",datas[0].perName);
    							onCreditForm.setValue("phone",datas[0].phone);
    							onCreditForm.setValue("onCreditMoney",datas[0].onCreditMoney);
    							onCreditForm.setDisabled(true);
    							widget.show([".J-onCreditForm"]);
    							break;
    						}
    					};
    					
    					$.each(datas[0].roomPersonalInfos,function(i,n){
    						n.checkOutSettlement.sum += n.damagesConsumption + n.roomConsumption;
    						n.checkOutSettlement.balance = n.checkOutSettlement.sum
    						- n.checkOutSettlement.bedSubsidy
    						- n.checkOutSettlement.coupon
    						- n.checkOutSettlement.expense;
    						n.liveDays = liveDays;
    						n.disabled = true;
    					});
    					//查询预收款
    					getSignupCharge(datas[0].roomPersonalInfos,function(tdata){
    						widget.set("endGridData",tdata);
    						endGrid.setData(tdata);
    						total.total(widget,tdata,data);
    					});
    				}
            	});
            	
				form.setDisabled(true);
				widget.show([".J-form",".J-endGrid",".J-total"]).hide([".J-grid","J-detailForm","J-alltotal"]);
				widget.get("subnav").show(["return","totalprint"]).hide(["paymentMethod","checkOutDate","CheckInStatus","servicepoint","print"]);
			}else{
				aw.ajax({
					url:"api/alliancesettlement/querysetl",
    				data : {
    					pkGroupReserve:data.groupPk,
    					fetchProperties:"signUpNumber,allianceSettlement.operator.pkUser,allianceSettlement.operator.name," +
    	    			"allianceSettlement.settlementDate,allianceSettlement.version,allianceSettlement.pkAllianceSettlement," +
    	    			"allianceSettlement.paymentMethods,allianceSettlement.balance,allianceSettlement.description," +
    	    			"allianceConfirm.pkAllianceConfirm,allianceConfirm.groupReserve.checkInDate,allianceConfirm.groupReserve.checkOutDate," +
    	    			"allianceConfirm.groupReserve.pkGroupReserve,allianceConfirm.groupReserve.planItemGroup.pkPlanItemGroup," +
    	    			"allianceConfirm.groupReserve.planItemGroup.planItem.destination.name," +
    	    			"allianceConfirm.groupReserve.planItemGroup.planItem.startDate,allianceConfirm.groupReserve.checkInDate," +
    	    			"allianceConfirm.groupReserve.checkOutDate,allianceConfirm.groupReserve.planItemGroup.servicePoint.name",
    				},
    				dataType : "json",
    				success : function(datas){
    		    		widget.hide([".J-grid"]).show([".J-allform",".J-editgrid",".J-alltotal"]);
    		    		widget.get("subnav").show(["return","totalprint"]).hide(["paymentMethod","servicepoint","CheckInStatus","checkOutDate","print"]);
    		    		widget.get("allform").setData(datas[0]);
    		    		widget.get("editgrid").refresh(null,function(param){
    		    			alltotal.total(widget,datas[0]);
    		    			widget.get("alltotalGrid").setDisabled(true);
    		    			widget.get("editgrid").setDisabled(true);
    		    		});
    		    	
    				}
				});
			}
            }
        },
        form: {
        	handleSelectRoomClick: function (widget) {
        		
            }
        }
    };
});